<template>
  <div class="address-node">
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
import areaList from '../Area.js'
export default {
  name: '',
  data: () => ({
    areaList,
    searchResult: []
  }),
  methods: {
    onSave(e) {
      console.log(e)
      let data = {}
      data.name = e.name
      data.tel = e.tel
      data.address = e.province + e.city + e.county + e.addressDetail + ''
      data.addressDetail = e.addressDetail
      data.areaCodeL = e.areaCode
      data.postalCode = e.postalCode
      data.isDefault = e.isDefault

      let addressData = JSON.parse(localStorage.getItem('address')) || []
      addressData.unshift(data)
      localStorage.setItem('address', JSON.stringify(addressData))

      console.log(data)
      this.$router.push('/usermy/order')
      this.$Toast('恭喜您,已成功添加地址')
    },
    onDelete() {
      this.$Toast('删除失败')
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: '天安门广场男厕所2号蹲位',
            address: '北京市'
          },
          {
            name: '麓谷企业广场千锋教育',
            address: '长沙市岳麓区'
          },
          {
            name: '梅溪湖新天地',
            address: '长沙市岳麓区'
          },
          {
            name: '苹果专卖店',
            address: '长沙市岳麓区'
          },
          {
            name: '小米之家',
            address: '长沙市岳麓区'
          }

        ]
      } else {
        this.searchResult = []
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
